package org.example.netty.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.example.utils.LogUtils;

public class AllocHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;

        LogUtils.info("入站数据：" + buf.readInt());

        // 新建一个缓冲区
        ByteBuf buffer = ctx.alloc().buffer();
        buffer.writeBytes("来自服务端的消息".getBytes());
        ctx.writeAndFlush(buffer);
        System.out.println(buffer);

        // 此处不能释放，释放之后引用计算为0导致出站数据无法读取
//        buffer.release();
        // 处理器截断，不往后传
        // super.channelRead(ctx, msg);
    }
}
